package com.youlai.boot.sport.controller;

import com.youlai.boot.sport.service.SportDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.sport.model.form.SportDataForm;
import com.youlai.boot.sport.model.query.SportDataQuery;
import com.youlai.boot.sport.model.vo.SportDataVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 运动数据前端控制层
 *
 * @author cuisheng
 * @since 2025-03-31 12:41
 */
@Tag(name = "运动数据接口")
@RestController
@RequestMapping("/api/v1/sportDatas")
@RequiredArgsConstructor
public class SportDataController  {

    private final SportDataService sportDataService;

    @Operation(summary = "运动数据分页列表")
    @GetMapping("/page")
    public PageResult<SportDataVO> getSportDataPage(SportDataQuery queryParams ) {
        IPage<SportDataVO> result = sportDataService.getSportDataPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增运动数据")
    @PostMapping
    public Result<Void> saveSportData(@RequestBody @Valid SportDataForm formData ) {
        boolean result = sportDataService.saveSportData(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取运动数据表单数据")
    @GetMapping("/{id}/form")
    public Result<SportDataForm> getSportDataForm(
        @Parameter(description = "运动数据ID") @PathVariable Long id
    ) {
        SportDataForm formData = sportDataService.getSportDataFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改运动数据")
    @PutMapping(value = "/{id}")
    public Result<Void> updateSportData(
            @Parameter(description = "运动数据ID") @PathVariable Long id,
            @RequestBody @Validated SportDataForm formData
    ) {
        boolean result = sportDataService.updateSportData(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除运动数据")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteSportDatas(
        @Parameter(description = "运动数据ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = sportDataService.deleteSportDatas(ids);
        return Result.judge(result);
    }
}
